package com.weng.platform.apiManager.doc.api;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.cloud.utils.CloudAuthUtil;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.web.base.BaseController;
import com.weng.framework.web.model.request.DeleteReqVo;
import com.weng.platform.apiManager.doc.entity.ApiDoc;
import com.weng.platform.apiManager.doc.model.vo.ApiDocVo;
import com.weng.platform.apiManager.doc.service.IApiDocService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


/**
 * api文档 API实现
 *
 * @author wengzhonghui
 * @date 2020-09-24 23:45:15
 *
 */  
@Slf4j
@RestController
@RequestMapping("/api-doc")
@Api(value = "api文档api", tags = {"api文档操作接口"})
public class ApiDocApi extends BaseController {
    @Autowired
    private IApiDocService apiDocService;

    /**
     * 功能描述:单个获取api文档
     */
    @ApiOperation(value = "获取api文档", notes = "")
    @GetMapping(value = "/get")
    public ResponseData get(String id){
        ApiDoc apiDoc = apiDocService.getById(id);
        return success(apiDoc);
    }


    /**
     * 条件 分页查询api文档
     */
    @ApiOperation(value = "分页获取api文档", notes = "")
    @GetMapping(value = "/list")
    public ResponseData list(Integer pageNo,Integer pageSize,String projectId, String title, String catalogId){
				IPage<ApiDoc> iPage = apiDocService.list(pageNo, pageSize, projectId, title, catalogId);
        return success(iPage);
    }


    /**
	 * 新增api文档
	 */
	@ApiOperation(value = "新增api文档", notes = "")
	@PostMapping(value = "/add")
	public ResponseData add(@Valid @RequestBody ApiDocVo apiDocReq, HttpServletRequest request){
	    String userId = CloudAuthUtil.getCurUserId(request);
	    String userName = CloudAuthUtil.getCurUserTrueName(request);
			ApiDoc apiDoc = Convert.convert(ApiDoc.class, apiDocReq);
			apiDoc.setAuthorId(userId);
			apiDoc.setLastUserId(userId);
			apiDoc.setAuthorUsername(userName);
			apiDoc.setLastUsername(userName);
			if(apiDocReq.getPosition() == null){
					apiDoc.setPosition(10.0);
			}
			apiDocService.add(apiDoc);
			return success();
	}

	/**
	 * 更新api文档
	 */
	@ApiOperation(value = "更新api文档", notes = "")
	@PostMapping(value = "/update")
	public ResponseData update(@Valid @RequestBody ApiDocVo apiDocReq, HttpServletRequest request){
		ApiDoc apiDoc = Convert.convert(ApiDoc.class, apiDocReq);
        String userId = CloudAuthUtil.getCurUserId(request);
        String userName = CloudAuthUtil.getCurUserTrueName(request);
        apiDoc.setLastUserId(userId);
        apiDoc.setLastUsername(userName);
		apiDocService.update(apiDoc);
		return success();
	}


	/**
	 * 删除api文档
	 */
	@ApiOperation(value = "ids 删除api文档", notes = "可以删除多个，中间以,号分隔")
	@PostMapping(value = "/delete")
	public ResponseData delete(@RequestBody DeleteReqVo deleteReq){
		String ids = deleteReq.getIds();
		if(StringUtils.isEmpty(ids)){
			return failed("删除失败,id不能为空！");
		}
		return apiDocService.removeByIds(ids);
	}
}
